package service.outputDocument;

import model.Blocks;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;



public class DataAnalyst {
    //Ａ４中的１８磅
    private static final int SIZESTANDARD= 22;
    private Blocks blocks;
    private Map<Integer, Integer> original;
    private Map<Integer, Integer> contain;

    DataAnalyst(Blocks blocks) {
        this.blocks=blocks;
        original = new TreeMap<Integer, Integer>();
        contain = new TreeMap<Integer, Integer>();

        int top;
        int height;
        int topNext;
        int bottom;
        int bottomNext;
        int size;
        int sizeNext;
        int sizeStandard;

        height=blocks.getDocImgHeight();
        sizeStandard = height/SIZESTANDARD;
        System.out.println(height);
        System.out.println("blocks"+blocks.getTextBlocks().size());

        //字号／ｈｅｉｇｈｔ
        //获取字符尺寸类型
        for (int i=0;i<blocks.getTextBlocks().size()-1;i++){

            top = blocks.getTextBlocks().get(i).getRect().getTop();
            bottom = blocks.getTextBlocks().get(i).getRect().getBottom();
//
            topNext = blocks.getTextBlocks().get(i+1).getRect().getTop();
            bottomNext = blocks.getTextBlocks().get(i+1).getRect().getBottom();

            size = height/(bottom-top);
            sizeNext = height/(bottomNext-topNext);


            if (size>sizeStandard&&sizeNext>sizeStandard){
                size=14;
            }else {
                size=20;
            }

            original.put(i,  size);
            contain.put(size,i);
//
            if (i==blocks.getTextBlocks().size()-2){
                sizeNext=size;
                original.put(i+1,sizeNext);
                contain.put(sizeNext,i+1);
            }
        }

        System.out.println("ｏriginal:"+original);
        System.out.println("contain"+contain.size());
    }

    List<Map<Integer, Integer>> analysisData(){
        if (contain.size()==2){
            analysisTitlePOS();
        }

        List<Map<Integer, Integer>> contentPos = new ArrayList<Map<Integer, Integer>>();
        if (0==contain.size()){
            System.out.println("数据错误");
        }
        if (2<contain.size()){
            System.out.println("数据错误");
        }
        //从ｏｒｉｇｉｎａｌ获取
        List<Integer> positionList;
        //从ｃｏｎｔａｉｎ　获取
        List<Integer> sizeList = new ArrayList<Integer>();
        sizeList.addAll(contain.keySet());
        System.out.println("字符大小"+sizeList);

        int h=0;
        for (Object sizeKey : sizeList){
            h++;
            //根据sizeList获取positionList

            positionList = getPositionList((TreeMap) original, sizeKey);
            System.out.println(sizeKey+"---字符位置的列表"+String.valueOf(positionList));

            //正文部分
            if (h==1){
                contentPos.add(buildPargaraphMap(positionList));
                System.out.println("正文派发成功"+positionList);
            }
            //标题
            if (h==2){
                Map<Integer, Integer> titlePos = new TreeMap<Integer, Integer>();
                for (Integer pos:positionList){
                    int i=0;
                    titlePos.put(i,pos);
                }
                contentPos.add(titlePos);
                System.out.println("主标题"+titlePos);
            }
        }
        return contentPos;
    }

    private void analysisTitlePOS() {
        List<Integer> list;
        list = getPositionList((TreeMap) original,20);

        for (Integer o:list){
            if (o>25){
                for (int i =0;i<blocks.getTextBlocks().size();i++){
                    ((TreeMap) original).replace(i,14);
                }
                contain.remove(20);
            }
        }


    }


    //将段落整体拆分出来
    private Map<Integer, Integer> buildPargaraphMap(List<Integer> positionList){

        List<Integer> pos = new ArrayList<Integer>();

        pos.addAll(positionList);
        System.out.println("pos:"+pos);

        System.out.println();

        //记录起始位置
        int i = positionList.get(0);
//        System.out.println("我是起始位置i："+i);
        //记录终止位置
        int j = i;
        int width;
        int posRight;
        int posNextLeft;
        int posBottom;
        int posNextBottom;
        int footleft;

        //保存段落开始和结束的映射
        Map<Integer, Integer> startEnd = new TreeMap<Integer, Integer>();
        Boolean signal=true;
        while (signal){

            width = blocks.getDocImgWidth();
//            System.out.println("我是width："+width);

            posRight  = blocks.getTextBlocks().get(j).getRect().getRight();
            posNextLeft = width-blocks.getTextBlocks().get(j+1).getRect().getLeft();
            posBottom = blocks.getTextBlocks().get(j).getRect().getBottom();
            posNextBottom = blocks.getTextBlocks().get(j+1).getRect().getBottom();
            //
            footleft= blocks.getTextBlocks().get(j).getRect().getLeft();
            int k = posRight+posNextLeft;
            int o = posNextBottom-posBottom;

//            System.out.println("我是K："+k);
//            System.out.println("我是Ｏ："+o);

            int p =blocks.getTextBlocks().get(j).getRect().getRight()-blocks.getTextBlocks().get(j).getRect().getLeft();
//            System.out.println("我是P："+p);

            if ((!(k<=width+5&&o<p))&&footleft<=width*0.85){
                startEnd.put(i,j);
                i=j+1;
//                System.out.println("我是起始位置i："+i);
            }

            j++;
            if (j==blocks.getTextBlocks().size()-1){
                startEnd.put(i,j);
                signal = false ;
            }
        }
        System.out.println("startEnd："+startEnd);
//        System.out.println(blocks.getTextBlocks().get(104).getContent());
//        System.out.println(blocks.getTextBlocks().get(105).getContent());


        return startEnd;
    }
    private List<Integer> getPositionList(TreeMap map, Object value){

        //输出多个values对应的key值
        ArrayList<Integer> keyList = new ArrayList<Integer>();

        for(Object getKey: map.keySet()){

            if(map.get(getKey).equals(value)){
                keyList.add((Integer) getKey);
            }
        }
        return keyList;
    }
}
